package com.wanghua.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.Query;
import com.wanghua.common.utils.R;
import com.wanghua.mall.product.dao.SkuInfoDao;
import com.wanghua.mall.product.entity.SkuInfoEntity;
import com.wanghua.mall.product.frontvo.SecKillSkuVo;
import com.wanghua.mall.product.frontvo.SkuItemVo;
import com.wanghua.mall.product.openfeign.SecKillClient;
import com.wanghua.mall.product.service.*;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


@Service
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Resource
    private SkuImagesService skuImagesService;
    @Resource
    private SpuInfoDescService spuInfoDescService;
    @Resource
    private AttrGroupService attrGroupService;
    @Resource
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;
    @Resource
    private SecKillClient secKillClient;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    /**
     * 条件列表
     */
    @Override
    public PageUtils queryPageCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        System.out.println(params);
        //分类
        String categoryId = (String) params.get("categoryId");
        if (StringUtils.hasLength(categoryId) && !categoryId.equals("0")) {
            queryWrapper.eq("category_id", categoryId);
        }
        //品牌
        String brandId = (String) params.get("brandId");
        if (StringUtils.hasLength(brandId) && !brandId.equals("0")) {
            queryWrapper.eq("brand_id", brandId);
        }
        //关键字
        String key = (String) params.get("key");
        if (StringUtils.hasLength(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.like("sku_name", key).or().like("sku_desc", key);
            });
        }
        //价格区间
        String min = (String) params.get("min");
        if (StringUtils.hasLength(min) && new BigDecimal(min).compareTo(new BigDecimal(0)) >= 0) {
            queryWrapper.and((wrapper) -> {
                wrapper.ge("price", new BigDecimal(min));
            });
        }
        String max = (String) params.get("max");
        if (StringUtils.hasLength(max) && new BigDecimal(max).compareTo(new BigDecimal(0)) >= 0) {
            queryWrapper.and((wrapper) -> {
                wrapper.le("price", new BigDecimal(max));
            });
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    //根据spuID查出所有sku
    @Override
    public List<SkuInfoEntity> getSkuBySpuId(Long spuId) {
        return baseMapper.selectList(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

    //获取商品详情页
    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        //1、sku基本信息的获取  pms_sku_info
        CompletableFuture<SkuInfoEntity> skuInfoEntityCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = baseMapper.selectById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);

        //3、获取spu的销售属性组合-> 依赖1 获取spuId
        CompletableFuture<Void> saleAttrFuture = skuInfoEntityCompletableFuture.thenAcceptAsync((res) -> skuItemVo.setSaleAttr(skuSaleAttrValueService.getAttrBySpuId(res.getSpuId())), threadPoolExecutor);
        //4、获取spu的介绍-> 依赖1 获取spuId
        CompletableFuture<Void> descFuture = skuInfoEntityCompletableFuture.thenAcceptAsync((res) -> skuItemVo.setDesc(spuInfoDescService.getById(res.getSpuId())), threadPoolExecutor);
        //5、获取spu的规格参数信息-> 依赖1 获取spuId categoryId
        CompletableFuture<Void> baseAttrFuture = skuInfoEntityCompletableFuture.thenAcceptAsync((res) -> skuItemVo.setGroupAttrs(attrGroupService.getAllAttrGroupAndAttrBySpuId(res.getSpuId())), threadPoolExecutor);

        //2、sku的图片信息    pms_sku_images
        CompletableFuture<Void> imagesFuture = CompletableFuture.runAsync(() -> skuItemVo.setImages(skuImagesService.getImagesBySkuId(skuId)), threadPoolExecutor);
        //TODO 6、秒杀商品的优惠信息
        CompletableFuture<Void> secKillFuture = CompletableFuture.runAsync(() -> {
            R r = secKillClient.getSecKillSkuInfo(skuId);
            SecKillSkuVo data = (SecKillSkuVo) r.get("data");
            skuItemVo.setSecKillSkuVo(data);
        }, threadPoolExecutor);

        //等待所有
        try {
            CompletableFuture.allOf(saleAttrFuture, baseAttrFuture, descFuture, imagesFuture, secKillFuture).get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return skuItemVo;
    }

}